const app= getApp();
const util=require('../../util.js');
const tabService = require("../../libs/tabService");

Page({

    /**
     * 页面的初始数据
     */
    data: {
        couponStatus:0,
        statusTxt:'',
        shared:0,
        bindPhone:0,
        modelsIndex:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('-----coupon---options----')
        console.log(options)
        console.log('----coupon----options----')
        let that=this;
        var parent_id=options.parent_id;
        var share=options.share;
        var store_id=options.id;
        var sn=options.sn;
        if(store_id!=undefined){
            
          console.log('store_id1=>'+store_id)
          this.setData({
            store_id:store_id
          });
          wx.setStorageSync('store_id',store_id);
         
      }else{
        store_id=options.store_id;
        if(store_id==undefined){
            store_id=wx.getStorageSync('store_id');
        }
        wx.setStorageSync('store_id',store_id);
        console.log('store_id2=>'+store_id)
        this.setData({
              store_id:store_id
            });        
      }
        if(share==undefined){
            share=0;
        }
        if(parent_id!=undefined){
            this.setData({
                parent_id:parent_id
              })
              wx.setStorageSync('parent_id',parent_id);
        }else{
            parent_id = wx.getStorageSync('parent_id');
        
        }
        var show = wx.getStorageSync('show');
        var enterOptions =wx.getEnterOptionsSync();
        console.log(enterOptions);
        var query=enterOptions.query;
        if(Object.keys(query).length === 0){
            console.log('----share index----query----');
            
        }else{
            console.log(query)
            if(query.hasOwnProperty('parent_id')){
                console.log('---has parent_id---');
                console.log(query.parent_id);
            }
            if(query.hasOwnProperty('store_id')){
                console.log('---has store_id---');
                console.log(query.store_id);
            }
            if(query.hasOwnProperty('help')){
                console.log('---has help---');
                console.log(query.help);
                this.setData({
                    help:query.help
                })
                wx.setStorageSync('help',query.help)
            }
            //console.log('none');
        }
        console.log('----share-- index----enterOptions----');
   
   
        var open=wx.getStorageSync('open');
        if(open==""){
            wx.navigateTo({
              url: '../login/index?parent_id='+parent_id,
            })
        }
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })       
          var pages = getCurrentPages();
      console.log(pages);      
        that.setData({
            share:share,
            capsule:app.globalData.capsule,
            platform:app.globalData.platform,    
            open:open,
             width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.platUrl
          })
    },
      goBack:function(){
        wx.navigateBack();
      },
      goIndex:function(){
        wx.reLaunch({
          url: '../index/index',
        })
      },
      goto:function(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        var index=e.currentTarget.dataset.index;
        var parent_id=wx.getStorageSync('parent_id');
        //console.log(index)
        if(url=="#"){

        }else if(url=='phone'){
            wx.makePhoneCall({
                phoneNumber: this.data.store.phone,
             })
        }else if(url=='coupon'){
            var date = util.formatTime(new Date());
            var dataset = e.currentTarget.dataset;
            var face_value = dataset.face_value;
            var pay_money = dataset.pay_money;
            var yxq = dataset.yxq;
            var month = dataset.month;
            var type = dataset.type;
            var send_status = dataset.send_status;
           //  log.info("Time:"+date+",page:index,function:goto:,index"+index);
            this.getCoupon(face_value,month,yxq,pay_money,type,send_status);
        }else{
            if(index==1){
                wx.reLaunch({               
                    url: url,
                  })
            }else{
                wx.navigateTo({
                    url: url,
                  })
            }
           
        }
        console.log(url);
    },

    getIndex:function(parent_id){
        let that = this;    
        console.log('coupon----getIndex')  
        var store_id = wx.getStorageSync('store_id');
        var open = wx.getStorageSync('open');
        wx.request({
          url: app.globalData.platUrl+'coupon/getIndexs',
          data:{from:1,user_id:open.user_id,parent_id:parent_id,store_id:store_id},
          success:res=>{
              console.log(res)
              var data=res.data;
              var model=[];
              var models=data.models;
              console.log(data);
              models.forEach(function(item,index){
                  model[index]=item;
              });
            that.setData({
             models:model,
             shareTitle:data.shareTitle,
             shareLogo:data.shareLogo,
             shareIndex:data.shareIndex,
              data:data.index,
              store:data.store,
              parent:data.parent,
              showImg:data.show,
              showArray:data.showArray
            })
          }
      })

    },
  
    modelsChange:function(e){
        //console.log(e)
        let that=this;
        var index=e.detail.value;
        var modelsIndex=this.data.modelsIndex;
        var open=wx.getStorageSync('open');        
        if(index>0){
            if(index!=modelsIndex){
                this.setData({
                    modelsIndex:index
                })
            }
        }else{
            wx.showToast({
              title: '请选择意向车型',
            })
        }
 
    },
    getModel:function(){
       this.setData({
           getmodel:true
       }) 
    },
    closeModel:function(){
        this.setData({
            modelsIndex:0,
            getmodel:false
        })
    },
    getCoupon:function(face_value,month,yxq,pay_money,type,send_status =1){
        let that = this;   
        var open = wx.getStorageSync('open'); 
        var user_id = open.user_id;
        var uid = open.uid;
        console.log(uid)
        //user_id = 0;
        if(user_id==0 || user_id==undefined){
      
            app.goto("../mine/bindPhone",'#');
            return;
        }
        var parent_id = wx.getStorageSync('parent_id');
        if(parent_id == undefined){
            parent_id=0;
        }
        var store_id=wx.getStorageSync('store_id');
        if(store_id==undefined){
            store_id=0;
        }
        if(store_id==''){
            store_id=0;
        }
        var modelIndex=this.data.modelsIndex;
        var phone=open.phone;
        var couponStatus=this.data.couponStatus;
        console.log(phone);
        // phone='';
        if(phone){
            var date = new Date();
            var hour = date.getHours();
            //alert(hour);
            //hour=7;
            console.log(hour)
 
                wx.request({
                    url: app.globalData.platUrl+'coupon/getCoupon',
                    data:{
                        from:1,
                        parent_id:parent_id,
                        user_id:user_id,
                        store_id:store_id,
                        face_value:face_value,
                        pay_money,
                        month:month,
                        yxq:yxq,
                        type:type,
                        send_status:send_status,
                        models:modelIndex,
                        couponStatus:couponStatus},
                    success:res=>{
                        console.log(res.data);
                        var data=res.data;
                        that.showModels(data.msg,data.url,data.index);
                    }
                })
            
        }else{
         that.setData({
             bindPhone:1
         })
        }
    },
    showModels:function(msg,url,index){
        console.log(url)
        wx.showModal({
          cancelColor: 'cancelColor',
          title:'系统提示',
          content:msg,
          //showCancel:false,
          success:function(e){
         
               if(url!="#"){
                   if(index== 'index'){
                    wx.reLaunch({
                        url: url,
                      })  
                   }else{
                    wx.navigateTo({
                        url: url,
                      })
                   }
                  
               }
              
          }
          

        })

    },
    cancel(){
        this.setData({
            bindPhone:0
        })
    },
    getPhoneNumber (e){
        let that=this;
        var open=wx.getStorageSync('open');
        console.log(e);
       
                wx.request({
                    url:app.globalData.platUrl+'login/getPhone',
                    data:{
                      code:e.detail.code,
                       open_id:open.user_id,
                       type:3
                    },
                    method:'GET',
                    header:{
                        'content-type':'application/json'
                    },
                    success:function(res){
                      console.log(res)
                        //wx.setStorageSync('PhoneNumber',res.data.phone);
                        if(res.data.code==200){
                          open.phone = res.data.phone;
                          wx.setStorageSync('open', open);
                          that.setData({
                              open:open,
                              bindPhone:0,
                              phone:res.data.phone
                          })
                          that.showModels('获取手机号成功',"#");
                        }else{
                            that.showModels('获取手机号失败','#');
                        } 
                        //that.show();
                        //console.log("手机号为");
                        //console.log(res);
                    },
                    fail:function(err){
                        console.log(err);
                    }
                })
       
      },
      refuse(e){ 
        //wx.setStorageSync('help', 0);
        this.setData({
            help:0
        })
      },
      help(e){
          var parent_id = this.data.parent_id;
          var open= wx.getStorageSync('open');
          console.log(open)
          let that = this;
          var user_id = open.user_id;
          var open_id = open.id;
         // user_id =1;
          if(parent_id){
              wx.request({
                  url: app.globalData.platUrl+'share/help',
                  data:{from:1,user_id:user_id,open_id:open_id,parent_id:parent_id},
                  success:res=>{
                      console.log(res);
                      let data= res.data;
                    if(res.statusCode==200){
                      app.showModals(data.msg)
                    }
                  }
            })
          }
          this.setData({
              help:0
          });
          if(parent_id>0){
            wx.setStorageSync('help', 1);
          }
          
          wx.setStorageSync('show', 0)
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var role = wx.getStorageSync('role');   
        var  tab = wx.getStorageSync('tabBar'); 
        console.log(tab)
        tabService.updateTabArray(this,tab);
        
        this.update(role) 
        var tabIndex=wx.getStorageSync('tabIndex');   
        tabService.updateIndex(this, tabIndex);     
        var parent_id=wx.getStorageSync('parent_id');
        let that=this;
        parent_id=parent_id?parent_id:0;
        this.getIndex(parent_id);
        /*
            that.data.couponTimer = setInterval(
            function () {
                 that.toClock();        
            }, 2000);
    
            that.setData({
                couponTimer:that.data.couponTimer
            });
        */
        },
        update(role) {
          tabService.updateRole(this, role);
        },
    toClock:function(){
            //console.log('111');
            let that = this;
            var open = wx.getStorageSync('open');
            var user_id = open.user_id;
      
              wx.request({
                    url: app.globalData.platUrl+'coupon/check',
                    data:{from:1,user_id:user_id},
                    success:res=>{
                        var data = res.data;
                      console.log(data);
                      that.setData({
                        couponStatus:data.couponStatus,
                        statusTxt:data.statusTxt,
                      })
                    }
                })
              
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        console.log(this.data.couponTimer);
        clearInterval(this.data.couponTimer);
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        clearInterval(this.data.couponTimer);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareTimeline(e){
        //console.log(e);
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareLogo;
        var open=wx.getStorageSync('open');
        var store_id = wx.getStorageSync('store_id');
        var parent_id=open.user_id;
         this.setData({
           share:true
         });
         console.log(shareLogo);
         console.log(title);
         return{
           title: title,                 
           query: 'share=4&help=1&parent_id='+parent_id+'&store_id='+store_id,        
           imageUrl:shareLogo      
           }
       },
       onShareAppMessage(e) {
        var open = wx.getStorageSync('open');
        var parent_id=open.user_id;
        var store_id = wx.getStorageSync('store_id');
        var that = this;
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareIndex;
   　  　var shareObj = {
   　　　　title: title,    
   　　　　path: '/pages/index/index?share=4&help=1&parent_id='+parent_id+'&store_id='+store_id,   
   　　　　imageUrl: shareLogo 
   　　　
   　　}
   　　// 来自页面内的按钮的转发
   　　if( e.from == 'button' ){
     　　　shareObj.title=title;
   　　　　shareObj.path = '/pages/index/index?share=4&help=1&parent_id='+parent_id+'&store_id='+store_id;
   　　}
   console.log(shareObj);
   　　// 返回shareObj
   　　return shareObj;
    }
})